/************************************************************************
 * File: gamemanager.cpp                                                *
 *                                                                      *
 * Copyright 2006, 2008 Colin Kern (colin.kern@gmail.com)               *
 *                                                                      *
 * This file is part of Profit.  Some previous versions were released   *
 * under the names Megacorp Mania and Acquire.                          *
 *                                                                      *
 * Profit is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU General Public License as published by *
 * the Free Software Foundation, either version 3 of the License, or    *
 * any later version.                                                   *
 *                                                                      *
 * Profit is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
 * GNU General Public License for more details.                         *
 *                                                                      *
 * You should have received a copy of the GNU General Public License    *
 * along with Profit.  If not, see <http://www.gnu.org/licenses/>.      *
 ************************************************************************/
#include "gamemanager.h"
#include "game.h"
#include "gameloader.h"
#include "player.h"
#include "resultsviewer.h"
#include "titlescreen.h"
#include "gamesetup.h"
#include <QHBoxLayout>
#include <QFile>
#include <QPainter>
#include <QSplitter>
#include <QTextEdit>
#include <QString>
#include <QStackedLayout>
#include <QScrollBar>

GameManager::GameManager(QWidget *parent) : QWidget(parent)
{
	m_results = new ResultsViewer;
	
	m_title = new TitleScreen;
	
	m_gameSetup = new GameSetup(this);
	connect(m_gameSetup, SIGNAL(setupDone()), this, SLOT(startGame()));
	
	m_stack = new QStackedLayout;
	m_stack->addWidget(m_results);
	m_stack->addWidget(m_title);
	m_stack->addWidget(m_gameSetup);
	
	m_messageLog = new QTextEdit();
	m_messageLog->setReadOnly(true);
	
	m_stack->setContentsMargins(0, 0, 0, 0);
	setLayout(m_stack);
	
	m_gameRunning = false;
	m_stack->setCurrentWidget(m_title);
}

QTextEdit *GameManager::getMessageLog() const
{
	return m_messageLog;
}

bool GameManager::gameRunning() const
{
	return m_gameRunning;
}

QSize GameManager::sizeHint() const
{
	return QSize(600, 450);
}

void GameManager::sortPlayers(Sort sort)
{
	QList<Player*> sorted;
	while (!m_players.empty()) {
		QListIterator<Player*> i(m_players);
		Player *max = i.next();
		while (i.hasNext()) {
			Player *player = i.next();
			if ((sort == Money && player->getMoney() > max->getMoney())) {
				max = player;
			}
		}
		sorted.append(max);
		m_players.removeAll(max);
	}
	m_players = sorted;
}

void GameManager::newGame()
{
	if (m_stack->indexOf(m_game) >= 0) {
		m_stack->removeWidget(m_game);
		delete m_game;
	}
	m_game = new Game();
	connect(m_game, SIGNAL(gameEnded()), this, SLOT(gameOver()));
	connect(m_game, SIGNAL(message(const QString&)),
		this, SLOT(addMessage(const QString&)));
	m_stack->addWidget(m_game);
	GameLoader loader(m_game);
	QFile *file = new QFile("data/standard.xml");
	file->open(QFile::ReadOnly | QFile::Text);
	/*
	if (!file->open(QFile::ReadOnly | QFile::Text)) {
		return 0;
	}
	*/
	loader.read(file);
	m_stack->setCurrentWidget(m_gameSetup);
}

void GameManager::addPlayer(const QString &name, bool computer)
{
	m_players.append(new Player(name, computer));
	m_game->addPlayer(m_players.last());
}

void GameManager::startGame()
{
	m_game->setupGUI();
	m_stack->setCurrentWidget(m_game);
	m_gameRunning = true;
	m_game->reset();
}

void GameManager::gameOver()
{
	m_game->hide();
	sortPlayers(Money);
	m_results->setMode(ResultsViewer::GameResult, m_players);
	m_stack->setCurrentWidget(m_results);
	m_gameRunning = false;
	update();
}

void GameManager::addMessage(const QString &message)
{
	m_messageLog->insertHtml(message + "<br>");
	m_messageLog->verticalScrollBar()
		->setValue(m_messageLog->verticalScrollBar()->maximum());
}
